package com.example.wenliangjiudian.controller;

import com.example.wenliangjiudian.entity.Room;
import com.example.wenliangjiudian.entity.User;
import com.example.wenliangjiudian.pojo.RoomDO;
import com.example.wenliangjiudian.service.RoomService;
import com.example.wenliangjiudian.service.UserService;
import com.example.wenliangjiudian.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoomService roomService;

    @RequestMapping("/404")
    public String error() {
        return "404";
    }


    @RequestMapping("/user/login")
    //@ResponseBody
    public Object login(String username, String password, HttpServletRequest request) {
        System.out.println("进入登陆");
        if (username == null || password == null) {
            request.setAttribute("message","用户名或密码不能为空");
            return "index";
        }

        User user = userService.getByName(username);
        if (user == null) {
            //用户不存在
            request.setAttribute("message","用户或密码不正确");
            return "index";
        }
        if (!user.getPassword().trim().equals(password.trim())) {
            //密码不正确
            request.setAttribute("message","用户或密码不正确");
            return "index";
        }

        //登录成功
        request.getSession().setAttribute("user", user);

        //拿到房间列表
        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomList", rooms);

        request.setAttribute("nickname", user.getNickname());
        request.setAttribute("headurl", user.getHeadurl());
        request.setAttribute("usertype", user.getUsertype());

        return "back/home";
    }


    @RequestMapping("/user/registerBtn")
    public Object registerBtn() {

        return "back/register";
    }

    @RequestMapping("/user/register")
    public Object register(User user, HttpServletRequest request) {
        String username = user.getUsername();
        User byName = userService.getByName(username);
        if (byName != null) {
            request.setAttribute("message", "用户已存在");
            return "back/register";
        }

        user.setUsertype(2);

        userService.register(user);

        return "index";
    }

    @RequestMapping("/root/index")
    public Object rootIndex( HttpServletRequest request) {
        //拿到房间列表
        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomList", rooms);

        return "back/home";
    }

    @RequestMapping("/root/quit")
    public Object quit(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return "index";
    }


    @RequestMapping("/user/list")
    public Object list(HttpServletRequest request) {
        List<User> list = userService.selectAll();

        request.setAttribute("list", list);
        return "back/user_info";
    }

    @RequestMapping("/user/delete")
    public Object delete(Integer id, HttpServletRequest request) {
        userService.delete(id);


        List<User> list = userService.selectAll();

        request.setAttribute("list", list);
        return "back/user_info";
    }

    @RequestMapping("/user/addBtn")
    public Object addBtn() {

        return "back/add_user";
    }


    @RequestMapping("/user/add")
    public Object add(User user, HttpServletRequest request) {
        String username = user.getUsername();
        User byName = userService.getByName(username);
        if (byName != null) {
            request.setAttribute("message", "用户已存在");
            return "back/add_user";
        }

        user.setUsertype(1);

        userService.add(user);
        List<User> list = userService.selectAll();
        request.setAttribute("list", list);
        return "back/user_info";
    }

    @RequestMapping("/user/updateBtn")
    public Object updateBtn(Integer id, HttpServletRequest request) {
        User user = userService.getById(id);
        request.setAttribute("user",user);
        return "back/update_user";
    }


    @RequestMapping("/user/update")
    public Object update(User user, HttpServletRequest request) {
        String username = user.getUsername();
        User byName = userService.getByName(username);
        if (byName != null && !byName.getId().equals(user.getId())) {
            request.setAttribute("message", "用户已存在");
            return "back/add_user";
        }


        userService.update(user);
        List<User> list = userService.selectAll();
        request.setAttribute("list", list);
        return "back/user_info";
    }
}
